
package com.venus.finance.fee.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.venus.common.action.BaseAction;
import com.venus.common.utils.ConditionUtil;
import com.venus.common.utils.Constants;
import com.venus.common.utils.DateUtil;
import com.venus.common.utils.MathUtil;
import com.venus.component.page.PageInfo;
import com.venus.finance.account.service.IAccountService;
import com.venus.finance.fee.form.FeeForm;
import com.venus.finance.fee.service.IFeeService;
import com.venus.finance.feetype.service.IFeeTypeService;
import com.venus.model.AccountVO;
import com.venus.model.FeeTypeVO;
import com.venus.model.FeeVO;

public class FeeMultiOperateAction extends BaseAction {

	private IFeeService feeService;
	
	private IAccountService accountService;
	
	private IFeeTypeService feeTypeService;
	
	public void setAccountService(IAccountService accountService) { this.accountService = accountService; }

	public void setFeeService(IFeeService feeService) {this.feeService = feeService;}
	
	public void setFeeTypeService(IFeeTypeService feeTypeService) {this.feeTypeService = feeTypeService;}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		FeeForm feeForm = (FeeForm)form;
		String strMethod = feeForm.getMethod();
		if("list".equals(strMethod)){
			return this.list(mapping, feeForm, request, response);
		}else if("create".equals(strMethod)){
			return this.create(mapping, feeForm, request, response);
		}else if("insert".equals(strMethod)){
			return this.insert(mapping, feeForm, request, response);
		}else if("edit".equals(strMethod)){
			return this.edit(mapping, feeForm, request, response);
		}else if("update".equals(strMethod)){
			return this.update(mapping, feeForm, request, response);
		}else if("delete".equals(strMethod)){
			return this.delete(mapping, feeForm, request, response);
		}else{
			this.log.error("没有找到访问的方法!方法名: "+strMethod);
			return null;
		}
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		FeeForm objFeeForm = (FeeForm)form;
		// 如果是从菜单过来的,清空form中的值
		this.saveToken(request);
		List<ConditionUtil> lstConditionUtils = new ArrayList<ConditionUtil>();
		// 条件查询
		// 类型
		if(Constants.FEE_TYPE_ALL != objFeeForm.getFeeType()){ // 2:所有类型
			lstConditionUtils.add(new ConditionUtil("and", "feeType.feeType", "=", objFeeForm.getFeeType()+"", false));
		}
		// 操作账户
		if(0 != objFeeForm.getAccountId()){
			lstConditionUtils.add(new ConditionUtil("and", "account.accountId", "=", objFeeForm.getAccountId()+"", false));
		}
		// 费用类型
		if(0 != objFeeForm.getFeeTypeId()){
			lstConditionUtils.add(new ConditionUtil("and", "feeType.feeTypeId", "=", objFeeForm.getFeeTypeId()+"", false));
		}
		// 费用子类
		if(0 != objFeeForm.getFeeSubTypeId()){
			lstConditionUtils.add(new ConditionUtil("and", "feeSubType.feeTypeId", "=", objFeeForm.getFeeSubTypeId()+"", false));
		}
		// 开始时间
		if(!"".equals(objFeeForm.getStartDate())){
			lstConditionUtils.add(new ConditionUtil("and", "feeDate", ">=", objFeeForm.getStartDate()+"", true));
		}
		// 结束时间
		if(!"".equals(objFeeForm.getEndDate())){
			lstConditionUtils.add(new ConditionUtil("and", "feeDate", "<=", objFeeForm.getEndDate()+"", true));
		}
		// 金额范围:最小值
		if(0 != objFeeForm.getMinAmount()){
			lstConditionUtils.add(new ConditionUtil("and", "amount", ">=", objFeeForm.getMinAmount()+"", false));
		}
		// 金额范围:最大值
		if(0 != objFeeForm.getMaxAmount()){
			lstConditionUtils.add(new ConditionUtil("and", "amount", "<=", objFeeForm.getMaxAmount()+"", false));
		}
		// 只显示当前用户的数据
		lstConditionUtils.add(new ConditionUtil("and", "pubUser.userId", "=", this.getLoginUser(request).getUserId()+"", false));
		// 按日期倒序
		lstConditionUtils.add(new ConditionUtil("order by", "feeDate", "", "desc", false));
		// 得到分页信息
		PageInfo objPageInfo = this.feeService.getPageInfo(objFeeForm.getPageRecord(), objFeeForm.getCurPage(), lstConditionUtils);
		request.setAttribute("objPageInfo", objPageInfo);
		request.setAttribute("lstFeeVOs", objPageInfo.getLstRecords());
		return mapping.findForward("list");
	}
	
	public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		FeeForm feeForm = (FeeForm)form;
		this.saveToken(request);
		// 账户列表
		List<AccountVO> lstAccountVOs = this.accountService.findAccountByUser(this.getLoginUser(request));
		request.setAttribute("lstAccountVOs", lstAccountVOs);
		
		List<FeeTypeVO> lstFeeTypeVOs = null;
		if(0 == feeForm.getFeeType()){ // 支出的子类
			lstFeeTypeVOs = this.feeTypeService.findSubFeeType(new Integer(1000));
		}else{ //收入的子类
			lstFeeTypeVOs = this.feeTypeService.findSubFeeType(new Integer(2000));
		}
		request.setAttribute("feeType", feeForm.getFeeType());
		request.setAttribute("lstFeeTypeVOs", lstFeeTypeVOs);
		return mapping.findForward("create");
		
	}

	public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		FeeForm feeForm = (FeeForm)form;
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		
		// 查询页面选择的费用类型
		FeeTypeVO objFeeTypeVO = this.feeTypeService.getFeeType(feeForm.getFeeTypeId());
		FeeTypeVO objFeeSubTypeVO = this.feeTypeService.getFeeType(feeForm.getFeeSubTypeId());
		
		// 处理分账
		if(feeForm.getSplitType()!=null && !feeForm.getSplitType().equals("")){
			Date[] dates = null ; // 分账后的日期
			if(feeForm.getSplitType().equals(Constants.FINANCE_FEE_MONTH2DAY)){ // 月-日
				// 得到选择月份的天数
				dates = DateUtil.getMonthDayDates(feeForm.getFeeDate().substring(0, feeForm.getFeeDate().lastIndexOf("-")));
			}else if(feeForm.getSplitType().equals(Constants.FINANCE_FEE_YEAR2MONTH)){ // 年-月
				// 一年的月份
				dates = DateUtil.getYearMonthDates(feeForm.getFeeDate(), "yyyy-MM-dd");
			}
			// 待分账总费用 % 天数
			float fAmount = MathUtil.div(feeForm.getAmount(), dates.length, 2).floatValue();
			// 新增完成之后更新账户余额
			AccountVO objAccountVO = this.accountService.loadAccountVO(feeForm.getAccountId());
			if(feeForm.getFeeType() == 0){ //支出
				objAccountVO.subAmount(feeForm.getAmount());
			}else{ //收入
				objAccountVO.addAmount(feeForm.getAmount());
			}
			this.accountService.saveOrUpdateAccountVO(objAccountVO);
			for(int i=0;i<dates.length;i++){
				FeeVO objFeeVO = new FeeVO();
				objFeeVO.setPubUser(this.getLoginUser(request));
				objFeeVO.setFeeType(objFeeTypeVO);
				objFeeVO.setFeeSubType(objFeeSubTypeVO);
				objFeeVO.setAccount(objAccountVO);
				objFeeVO.setFeeDate(dates[i]);
				// 拆分后的金额
				objFeeVO.setAmount(fAmount);
				objFeeVO.setIsReturn(feeForm.getIsReturn());
				objFeeVO.setNote(feeForm.getNote());
				this.feeService.saveOrUpdateFeeVO(objFeeVO);
			}
		}else{ // 非分账处理
			FeeVO objFeeVO = new FeeVO();
			objFeeVO.setPubUser(this.getLoginUser(request));
			objFeeVO.setFeeType(objFeeTypeVO);
			objFeeVO.setFeeSubType(objFeeSubTypeVO);
			// 新增完成之后更新账户余额
			AccountVO objAccountVO = this.accountService.loadAccountVO(feeForm.getAccountId());
			if(feeForm.getFeeType() == 0){ //支出
				objAccountVO.subAmount(feeForm.getAmount());
			}else{ //收入
				objAccountVO.addAmount(feeForm.getAmount());
			}
			objFeeVO.setAccount(objAccountVO);
			objFeeVO.setFeeDate(DateUtil.parseDate(feeForm.getFeeDate(), "yyyy-MM-dd"));
			objFeeVO.setAmount(feeForm.getAmount());
			objFeeVO.setIsReturn(feeForm.getIsReturn());
			objFeeVO.setNote(feeForm.getNote());
			this.feeService.saveOrUpdateFeeVO(objFeeVO);
			this.accountService.saveOrUpdateAccountVO(objAccountVO);
			// 页面上新增记录展现特别标识
			request.setAttribute("newFeeId", objFeeVO.getFeeId());
		}
		// 参数清除,防止作为列表查询条件
		feeForm.reset(mapping, request);
		return this.list(mapping, feeForm, request, response);
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		this.saveToken(request);
		FeeForm feeForm = (FeeForm)form;
		FeeVO objFeeVO = this.feeService.getFeeVO(feeForm.getFeeId());
		request.setAttribute("objFeeVO", objFeeVO);
		List<AccountVO> lstAccountVOs = this.accountService.findAccountByUser(this.getLoginUser(request));
		request.setAttribute("lstAccountVOs", lstAccountVOs);
		// 判断是支出还是收入
		List<FeeTypeVO> lstFeeTypeVOs = null;
		if(objFeeVO.getFeeType().getFeeType() == 0){ //支出
			lstFeeTypeVOs = this.feeTypeService.findSubFeeType(new Integer(1000));
		}else{ //收入
			lstFeeTypeVOs = this.feeTypeService.findSubFeeType(new Integer(2000));
		}
		request.setAttribute("lstFeeTypeVOs", lstFeeTypeVOs);
		// 查询子类
		List<FeeTypeVO> lstSubFeeTypeVOs = this.feeTypeService.findSubFeeType(objFeeVO.getFeeType().getFeeTypeId());
		request.setAttribute("lstSubFeeTypeVOs", lstSubFeeTypeVOs);
		return mapping.findForward("edit");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		FeeForm feeForm = (FeeForm)form;
		FeeVO objFeeVO = this.feeService.getFeeVO(feeForm.getFeeId());
		objFeeVO.setPubUser(this.getLoginUser(request));
		objFeeVO.setFeeType(this.feeTypeService.getFeeType(feeForm.getFeeTypeId()));
		objFeeVO.setFeeSubType(this.feeTypeService.getFeeType(feeForm.getFeeSubTypeId()));
		// 跨帐处理,先将原先的账恢复,在修改新账
		
		// 原先的账号
		AccountVO objAccountVO = objFeeVO.getAccount();
		// 新关联的账号
		AccountVO objNewAccountVO = this.accountService.loadAccountVO(feeForm.getAccountId());
		// 判断支出,收入
		if(objFeeVO.getFeeType().getFeeType() == 0){ // 支出
			if(objAccountVO.getAccountId().intValue()!=objNewAccountVO.getAccountId().intValue()){
				// 原先的账恢复:账号余额+旧消费金额
				objAccountVO.addAmount(feeForm.getOldAmount());
				// 从新账号支出:新账号余额-新消费金额
				objNewAccountVO.subAmount(feeForm.getAmount());
			}else{
				// 原先的账恢复:账号余额+旧消费金额
				objAccountVO.addAmount(feeForm.getOldAmount());
				// 原先的账号支出:账号余额-新消费金额
				objAccountVO.subAmount(feeForm.getAmount());
			}
		}else{ // 收入
			if(objAccountVO.getAccountId().intValue()!=objNewAccountVO.getAccountId().intValue()){
				// 原先的账恢复:账号余额-老收入金额
				objAccountVO.subAmount(feeForm.getOldAmount());
				// 从新账号支出:新账号余额+新收入金额
				objNewAccountVO.addAmount(feeForm.getAmount());
			}else{
				// 原先的账恢复:账号余额-老收入金额
				objAccountVO.subAmount(feeForm.getOldAmount());
				// 原先的账号支出:账号余额+新收入金额
				objAccountVO.addAmount(feeForm.getAmount());
			}
		}
		// 把新关联的账号与费用关联
		objFeeVO.setAccount(objNewAccountVO);
		objFeeVO.setFeeDate(DateUtil.parseDate(feeForm.getFeeDate(), "yyyy-MM-dd"));
		objFeeVO.setAmount(feeForm.getAmount());
		objFeeVO.setIsReturn(feeForm.getIsReturn());
		objFeeVO.setNote(feeForm.getNote());
		this.feeService.saveOrUpdateFeeVO(objFeeVO);
		// 更新新账号
		this.accountService.saveOrUpdateAccountVO(objNewAccountVO);
		// 更新老账号
		this.accountService.saveOrUpdateAccountVO(objAccountVO);
		// 页面上已修改记录展现特别标识
		request.setAttribute("updateFeeId", objFeeVO.getFeeId());
		// 参数清除,防止作为列表查询条件
		feeForm.reset(mapping, request);
		return this.list(mapping, feeForm, request, response);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		FeeForm feeForm = (FeeForm)form;
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		this.feeService.deleteFeeVO(feeForm.getFeeIds());
		return this.list(mapping, form, request, response);
	}

}